//
//  LHQSearchViewController.m
//  Music
//
//  Created by dllo on 16/6/9.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LHQSearchViewController.h"
#import "LHQsearchTableViewCell.h"
#import "LHQlikeModel.h"
#import "LHQMusicOnViewController.h"
@interface LHQSearchViewController ()<UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,retain) UITableView  *tableView;
@property (nonatomic,assign) NSInteger  selected;
/** 网络请求得到的数组 */
@property (nonatomic, strong) NSMutableArray *sourceArray;
@property (nonatomic,retain) MBProgressHUD  *HUD;
@property (nonatomic,assign) NSInteger piNumber;
@property (nonatomic,assign) NSInteger  number;
@property (nonatomic,retain) NSString  *perPath;
@property (nonatomic,retain) UIImageView  *imageView;
@property (nonatomic,retain) NSString  *searchText;
@property (nonatomic,retain) UISearchBar  *myBar;
@property (nonatomic,retain) LHQlikeModel  *model;
@end

@implementation LHQSearchViewController
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.tabBarController.tabBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.selected = 10;
    self.view.backgroundColor = [UIColor  whiteColor];
    self.navigationController.navigationBar.barStyle = 1;
    self.navigationController.navigationBar.tintColor = [UIColor  whiteColor];
    
    
    
    
    self.myBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 25)];
    
    self.myBar.barTintColor = [UIColor whiteColor];//把黑色去掉
    //[[self.myBar.subviews objectAtIndex:0]removeFromSuperview];
    self.myBar.layer.borderWidth = 1;
    self.myBar.tintColor = [UIColor redColor];
    self.myBar.backgroundColor = [UIColor whiteColor];
    [self.myBar.layer setBorderColor:[UIColor lightGrayColor].CGColor];
    self.myBar.layer.cornerRadius = 10;
    self.myBar.layer.masksToBounds = YES;
    self.myBar.delegate = self;
    self.myBar.placeholder = @"搜索  歌名  姓名  ";
    self.myBar.keyboardType = UIKeyboardAppearanceDefault;//键盘
    [self.view  addSubview:self.myBar];
}
-(void)tableViewSubView
{
    
    self.tableView = [[UITableView  alloc]initWithFrame:CGRectMake(0, 0, Screenwidth, Screenheight-64-49) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor  whiteColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView  registerClass:[LHQsearchTableViewCell  class] forCellReuseIdentifier:@"search"];
   [self.view  addSubview:_tableView];
}
//单曲数据
-(void)getSingleDataSource
{
    self.sourceArray = [NSMutableArray  array];
    NSString  *url = [NSString  stringWithFormat:@"http://api.dongting.com/misc/search/song?q=%@&page=1&size=50&app=ttpod&v=v8.0.1.2015091618&uid=&mid=iPhone7,2&f=f320&s=s310&imsi=&hid=&splus=9.0.1&active=1&net=2&openudid=e65d9a0e3de1aa2b70ba313f1ca52326ce8e2e06&idfa=5B302CA5-E537-4B10-955D-520F4C91BAFE&utdid=VeQF/rl/rPcDAJS/IF9m1eH3&alf=201200&bundle_id=com.ttpod.music&latitude=38.88260962951725&longtitude=121.5397083338999",self.searchText];
    [MyTools  getNetWithUrl:url body:nil header:nil response:LHQJSON success:^(id result) {
        NSDictionary  *dic = (NSDictionary *)result;
        NSMutableArray  *temp = dic[@"data"];
        for (NSDictionary  *dict in temp) {
            self.model = [[LHQlikeModel alloc]initWithDic:dict];
            [self.sourceArray  addObject:_model];
        }
        [self tableViewSubView];
        [self.tableView  reloadData];
        
        
    } failure:^(NSError *error) {
        
    }];
}
//开始编辑是searchbar
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    
    searchBar.showsScopeBar = NO;
    [searchBar sizeThatFits:CGSizeMake(self.view.frame.size.width, 30)];
    [searchBar setShowsCancelButton:YES animated:YES];
    return YES;
}
//点击searchbar取消按钮事件
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [self.navigationController  popToRootViewControllerAnimated:NO];
}
//结束编辑
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
    
    searchBar.showsScopeBar = NO;//搜索范围
    
    [searchBar sizeToFit];
    
    [searchBar setShowsCancelButton:NO animated:YES];
    
    return YES;
    
}
//点击searchbar
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    self.searchText = self.myBar.text;
    [self  getSingleDataSource];
    [searchBar resignFirstResponder];//回收键盘
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.sourceArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
        LHQsearchTableViewCell   *cell = [tableView  dequeueReusableCellWithIdentifier:@"search"];
        
        self.model = [self.sourceArray  objectAtIndex:indexPath.row];
    
        cell.model = self.model;
        NSInteger  num = indexPath.row;
    
       cell.numLabel.text = [NSString  stringWithFormat:@"%ld",num];
    
        // 高亮
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.model.name];
        NSRange range = [self.model.name rangeOfString:self.myBar.text];
        [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
        cell.song_name.attributedText = attributedString;
        return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 50;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    
    LHQlikeModel  *model = self.sourceArray[indexPath.row];
    NSString  *url = model.auditionList[1][@"url"];
    NSString  *time = model.auditionList[1][@"duration"];
    NSString  *singer = model.singerName;
    LHQMusicOnViewController  *on = [[LHQMusicOnViewController  alloc] init];
    
    on.image = model.picUrl;
    on.title = model.name;
    on.url = url;
    on.DataSource = self.sourceArray;
    on.index = indexPath.row;
    on.time = time.description ;
    on.urlid = singer;
    self.navigationController.tabBarController.tabBar.hidden = YES;
    [self.navigationController  pushViewController:on animated:YES];
    

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
